<?php
Raise::load('core.RaiseObject');
Raise::load('core.RaiseSession');
Raise::load('core.RaiseTypedCollection');

/**
 * RaisePostbackFeedback class
 * A handler that handles postback errors
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.Postback
 * @since 1.2
 */
class RaisePostbackFeedback extends RaiseObject {

    const SESSION_KEY = '_?:RaisePostbackFeedback';
    
    /**
     * The list of field feedback for this postback
     * @var RaiseTypedCollection
     */
    private $feedback;

    /**
     * The RaiseSession that this exception handler is using
     * @var RaiseSession
     */
    private $session;

    /**
     * Create a new Postback exception handler
     * @param RaiseSession $session
     */
    public function __construct($session){
        $this->session($session);
        $this->feedback = new RaiseTypedCollection('RaiseFieldFeedback');
    }

    /**
     * Get the list of exceptions to the current postback
     * @return RaiseTypedCollection
     */
    public function feedback(){
        return $this->feedback;
    }

    /**
     * Get or set the RaiseSession object that this exception handler will use
     * @param RaiseSession $s (optional) Set the new RaiseSession object
     * @return RaiseSession
     */
    public function session($s = null){
        if(func_num_args() == 1){
            if($s instanceof RaiseSession){
                $this->session = $s;
            }else{
                throw new RaiseInvalidArgumentException('RaisePostbackExceptionHandler::session() expects first parameter to be of type RaiseSession. '. RaiseVariable::typeInfo($s) . ' given instead.');
            }
        }
        return $this->session;
    }

    public function save(){
        $list = $this->session->get(self::SESSION_KEY);
        if($list instanceof RaiseTypedCollection && $list->type() == 'RaiseFieldFeedback'){
            $list->append($this->feedback());
        }else{
            $list = $this->feedback();
        }
        $this->session->set(self::SESSION_KEY, $list);
    }

}